﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CommonLibraries.DeviceLibrary
{
    public class Handheld: IHandheld
    {
        #region IHandheld Members
        private static Handheld Instance = null;
        private static void CreateInstance()
        {
            if (Instance == null)
            {
                Instance = new Handheld();
            }
        }
        public static Handheld GetInstance()
        {
            if (Instance == null) CreateInstance();
            return Instance;
        }
        private Handheld()
        { }


        public event EventHandler BatteryLevelChanged;
        public event EventHandler PowerSourceChanged;


        public string SerialNumber
        {
            get{
                byte _SerialNumber;
                _SerialNumber = new byte();
                TGMS2.TGMS2_GetSerialNumber(_SerialNumber);
                return _SerialNumber.ToString();
            }
        }

        private PowerSources m_PreviousPowerSource;
        public PowerSources PowerSource
        {
            get
            {
                // TODO: implement
                PowerSources _PowerSource;
                int _batValue;
                _batValue = default(int);
                TGMS2.TGMS2_GetBatteryPowerValue((uint)TGMS2.TGMS2ENUM.TGMS2_AC_VOLTAGE, ref _batValue);

                if (_batValue > 790)
                    _PowerSource = PowerSources.PowerPlug;
                else
                    _PowerSource = PowerSources.Battery;
             
                
                //int ConverState  = TGMS2.TGMS2_GetBatteryCoverState();

                
                

                //if (_PowerSource != m_PreviousPowerSource)
                //    this.PowerSourceChanged(this, new EventArgs());
                //else
                //    this.PowerSourceChanged(null, null);
                //m_PreviousPowerSource = _PowerSource;

                return _PowerSource;
            }
        }
        private int m_PreviousBatteryLevel = default(int);
        public int BatteryLevel
        {
            get
            {
                int _batValue;
                _batValue = default(int);

                TGMS2.TGMS2_GetBatteryPowerValue((uint)TGMS2.TGMS2ENUM.TGMS2_MAIN_BATTERY, ref _batValue);

                if (m_PreviousBatteryLevel != _batValue && BatteryLevelChanged != null)
                    BatteryLevelChanged(this, new EventArgs());
                m_PreviousBatteryLevel = _batValue;
                return _batValue; 
            }
        }

        #endregion
    }
}
